<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>“Hello world” HTTP server &mdash; cpp-netlib v0.12.0</title>
    
    <link rel="stylesheet" href="../../_static/pyramid.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.12.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="cpp-netlib v0.12.0" href="../../contents.html" />
    <link rel="up" title="Examples" href="../../examples.html" />
    <link rel="next" title="“Hello world” HTTP client" href="hello_world_client.html" />
    <link rel="prev" title="Simple wget" href="simple_wget.html" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Neuton&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="../../_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->

  </head>
  <body role="document">

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="hello_world_client.html" title="“Hello world” HTTP client"
             accesskey="N">next</a></li>
        <li class="right" >
          <a href="simple_wget.html" title="Simple wget"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../contents.html">cpp-netlib v0.12.0</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="../../examples.html" accesskey="U">Examples</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="hello-world-http-server">
<span id="id1"></span><h1>&#8220;Hello world&#8221; HTTP server<a class="headerlink" href="#hello-world-http-server" title="Permalink to this headline">¶</a></h1>
<p>Now that we&#8217;ve seen how we can deal with request and response objects from the
client side, we&#8217;ll see how we can then use the same abstractions on the server
side. In this example we&#8217;re going to create a simple HTTP Server in C++ using
<code class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></code>.</p>
<div class="section" id="the-code">
<h2>The code<a class="headerlink" href="#the-code" title="Permalink to this headline">¶</a></h2>
<p>The <code class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></code> provides the framework to develop embedded HTTP
servers.  For this example, the server is configured to return a
simple response to any HTTP request.</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;boost/network/protocol/http/server.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>

<span class="k">namespace</span> <span class="n">http</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">network</span><span class="o">::</span><span class="n">http</span><span class="p">;</span>

<span class="k">struct</span> <span class="n">hello_world</span><span class="p">;</span>
<span class="k">typedef</span> <span class="n">http</span><span class="o">::</span><span class="n">server</span><span class="o">&lt;</span><span class="n">hello_world</span><span class="o">&gt;</span> <span class="n">server</span><span class="p">;</span>

<span class="k">struct</span> <span class="n">hello_world</span> <span class="p">{</span>
    <span class="kt">void</span> <span class="k">operator</span><span class="p">()(</span><span class="n">server</span><span class="o">::</span><span class="n">request</span> <span class="k">const</span> <span class="o">&amp;</span><span class="n">request</span><span class="p">,</span> <span class="n">server</span><span class="o">::</span><span class="n">response</span> <span class="o">&amp;</span><span class="n">response</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">server</span><span class="o">::</span><span class="n">string_type</span> <span class="n">ip</span> <span class="o">=</span> <span class="n">source</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
        <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="n">request</span><span class="p">.</span><span class="n">source_port</span><span class="p">;</span>
        <span class="n">std</span><span class="o">::</span><span class="n">ostringstream</span> <span class="n">data</span><span class="p">;</span>
        <span class="n">data</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Hello, &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">ip</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;:&#39;</span> <span class="o">&lt;&lt;</span> <span class="n">port</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;!&#39;</span><span class="p">;</span>
        <span class="n">response</span> <span class="o">=</span> <span class="n">server</span><span class="o">::</span><span class="n">response</span><span class="o">::</span><span class="n">stock_reply</span><span class="p">(</span><span class="n">server</span><span class="o">::</span><span class="n">response</span><span class="o">::</span><span class="n">ok</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
    <span class="p">}</span>
    <span class="kt">void</span> <span class="n">log</span><span class="p">(</span><span class="k">const</span> <span class="n">server</span><span class="o">::</span><span class="n">string_type</span><span class="o">&amp;</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;ERROR: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">message</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Usage: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; address port&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">try</span> <span class="p">{</span>
        <span class="n">hello_world</span> <span class="n">handler</span><span class="p">;</span>
        <span class="n">server</span><span class="o">::</span><span class="n">options</span> <span class="n">options</span><span class="p">(</span><span class="n">handler</span><span class="p">);</span>
        <span class="n">server</span> <span class="n">server_</span><span class="p">(</span><span class="n">options</span><span class="p">.</span><span class="n">address</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]).</span><span class="n">port</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]));</span>
        <span class="n">server_</span><span class="p">.</span><span class="n">run</span><span class="p">();</span>
    <span class="p">}</span>
    <span class="k">catch</span> <span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span> <span class="o">&amp;</span><span class="n">e</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This is about a straightforward as server programming will get in C++.</p>
</div>
<div class="section" id="building-and-running-the-server">
<h2>Building and running the server<a class="headerlink" href="#building-and-running-the-server" title="Permalink to this headline">¶</a></h2>
<p>Just like with the HTTP client, we can build this example by doing the following
on the shell:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ <span class="nb">cd</span> ~/cpp-netlib-build
$ make hello_world_server
</pre></div>
</div>
<p>The first two arguments to the <code class="docutils literal"><span class="pre">server</span></code> constructor are the host and
the port on which the server will listen.  The third argument is the
the handler object defined previously.  This example can be run from
a command line as follows:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ ./example/hello_world_server 0.0.0.0 8000
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you&#8217;re going to run the server on port 80, you may have to run it
as an administrator.</p>
</div>
</div>
<div class="section" id="diving-into-the-code">
<h2>Diving into the code<a class="headerlink" href="#diving-into-the-code" title="Permalink to this headline">¶</a></h2>
<p>Let&#8217;s take a look at the code listing above in greater detail.</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;boost/network/protocol/http/server.hpp&gt;</span><span class="cp"></span>
</pre></div>
</div>
<p>This header contains all the code needed to develop an HTTP server with
<code class="xref py py-mod docutils literal"><span class="pre">cpp-netlib</span></code>.</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">hello_world</span><span class="p">;</span>
<span class="k">typedef</span> <span class="n">http</span><span class="o">::</span><span class="n">server</span><span class="o">&lt;</span><span class="n">hello_world</span><span class="o">&gt;</span> <span class="n">server</span><span class="p">;</span>

<span class="k">struct</span> <span class="n">hello_world</span> <span class="p">{</span>
    <span class="kt">void</span> <span class="k">operator</span><span class="p">()(</span><span class="n">server</span><span class="o">::</span><span class="n">request</span> <span class="k">const</span> <span class="o">&amp;</span><span class="n">request</span><span class="p">,</span> <span class="n">server</span><span class="o">::</span><span class="n">response</span> <span class="o">&amp;</span><span class="n">response</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">server</span><span class="o">::</span><span class="n">string_type</span> <span class="n">ip</span> <span class="o">=</span> <span class="n">source</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
        <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="n">request</span><span class="p">.</span><span class="n">source_port</span><span class="p">;</span>
        <span class="n">std</span><span class="o">::</span><span class="n">ostringstream</span> <span class="n">data</span><span class="p">;</span>
        <span class="n">data</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Hello, &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">ip</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;:&#39;</span> <span class="o">&lt;&lt;</span> <span class="n">port</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;!&#39;</span><span class="p">;</span>
        <span class="n">response</span> <span class="o">=</span> <span class="n">server</span><span class="o">::</span><span class="n">response</span><span class="o">::</span><span class="n">stock_reply</span><span class="p">(</span><span class="n">server</span><span class="o">::</span><span class="n">response</span><span class="o">::</span><span class="n">ok</span><span class="p">,</span> <span class="n">data</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
    <span class="p">}</span>
    <span class="kt">void</span> <span class="n">log</span><span class="p">(</span><span class="k">const</span> <span class="n">server</span><span class="o">::</span><span class="n">string_type</span><span class="o">&amp;</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;ERROR: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">message</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">hello_world</span></code> is a functor class which handles HTTP requests.
All the operator does here is return an HTTP response with HTTP code 200
and the body <code class="docutils literal"><span class="pre">&quot;Hello,</span> <span class="pre">&lt;ip&gt;:&lt;port&gt;!&quot;</span></code>. The <code class="docutils literal"><span class="pre">&lt;ip&gt;</span></code> in this case would be
the IP address of the client that made the request and <code class="docutils literal"><span class="pre">&lt;port&gt;</span></code> the clients port.</p>
<p>There are a number of pre-defined stock replies differentiated by
status code with configurable bodies.
All the supported enumeration values for the response status codes can be found
in <code class="docutils literal"><span class="pre">boost/network/protocol/http/impl/response.ipp</span></code>.</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="n">hello_world</span> <span class="n">handler</span><span class="p">;</span>
<span class="n">server</span><span class="o">::</span><span class="n">options</span> <span class="n">options</span><span class="p">(</span><span class="n">handler</span><span class="p">);</span>
<span class="n">server</span> <span class="nf">server_</span><span class="p">(</span><span class="n">options</span><span class="p">.</span><span class="n">address</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]).</span><span class="n">port</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]));</span>
<span class="n">server_</span><span class="p">.</span><span class="n">run</span><span class="p">();</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">server</span></code> constructor requires an object of the <code class="docutils literal"><span class="pre">options</span></code> class,
this object stores all needed options, especially the host and
the port on which the server will listen.
The <code class="docutils literal"><span class="pre">options</span></code> constructor&#8217;s single argument is the handler object defined previously.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In this example, the server is specifically made to be single-threaded.
In a multi-threaded server, you would invoke the <code class="docutils literal"><span class="pre">hello_world::run</span></code> member
method in a set of threads. In a multi-threaded environment you would also
make sure that the handler does all the necessary synchronization for shared
resources across threads. The handler is passed by reference to the server
constructor and you should ensure that any calls to the <code class="docutils literal"><span class="pre">operator()</span></code> overload
are thread-safe.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">&#8220;Hello world&#8221; HTTP server</a><ul>
<li><a class="reference internal" href="#the-code">The code</a></li>
<li><a class="reference internal" href="#building-and-running-the-server">Building and running the server</a></li>
<li><a class="reference internal" href="#diving-into-the-code">Diving into the code</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="simple_wget.html"
                        title="previous chapter">Simple <cite>wget</cite></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="hello_world_client.html"
                        title="next chapter">&#8220;Hello world&#8221; HTTP client</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="hello_world_client.html" title="“Hello world” HTTP client"
             >next</a></li>
        <li class="right" >
          <a href="simple_wget.html" title="Simple wget"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../contents.html">cpp-netlib v0.12.0</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="../../examples.html" >Examples</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2008-2014, Glyn Matthews, Dean Michael Berris; 2013 Google, Inc..
      Last updated on Mar 29, 2016.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
    </div>
  </body>
</html>